﻿// Sheego.Service.X400
// Copyright(C) 2015 Schwab Versand GmbH
//
// This source file is subject to the Microsoft Public License(Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Sheego.Service.X400.Job.Shared.Locator;
using Sheego.Service.X400.Job.Shared.Parser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sheego.Service.X400.Job.Impl.Parser
{
    class X400AddressParser : IX400AddressParser
    {
        #region IX400AddressParser Members

        public Shared.BO.IX400Address Parse(string address)
        {
            if (String.IsNullOrWhiteSpace(address))
                throw new ArgumentNullException("address");


            var x400Address = JobLocator.GetX400Address();
            x400Address.Administrationmanagementdomain = "";
            x400Address.Countryname = "";
            x400Address.DDA_RFC_822 = "";
            x400Address.FREEFORM = "";
            x400Address.Givenname = "";
            x400Address.Initials = "";
            x400Address.OrganizationalUnitnames = "";
            x400Address.Organizationname = "";
            x400Address.Surname = "";

            foreach (var part in address.Split(';'))
            {
                var ix = part.IndexOf('=');
                if (ix < 0) throw new ArgumentException(address);
                var Key = part.Substring(0, ix).ToUpper();
                var Value = part.Substring(ix + 1);

                switch (Key)
                {
                    case "A":
                        x400Address.Administrationmanagementdomain = Value;
                        break;
                    case "C":
                        x400Address.Countryname = Value;
                        break;
                    case "DDA:RFC-822":
                        x400Address.DDA_RFC_822 = Value;
                        break;
                    case "FREEFORM":
                        x400Address.FREEFORM = Value;
                        break;
                    case "O":
                        x400Address.Organizationname = Value;
                        break;
                    case "OU":
                        x400Address.OrganizationalUnitnames = Value;
                        break;
                    case "S":
                        x400Address.Surname = Value;
                        break;
                    case "G":
                        x400Address.Givenname = Value;
                        break;
                    case "I":
                        x400Address.Initials = Value;
                        break;
                    default:
                        throw new ArgumentException(part);
                }

            }
            return x400Address;

        }

        #endregion
    }
}
